package org.blame.springredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping("/testString")
    private String testString(){
        redisTemplate.opsForValue().set("key","11");
        redisTemplate.opsForValue().set("key2","22");
        redisTemplate.opsForValue().set("key3","33");

        String s = redisTemplate.opsForValue().get("key");
        System.out.println(s);
        return "";
    }
    @RequestMapping("/testList")
    @ResponseBody
    public String testList(){
        redisTemplate.execute((RedisConnection redisConnection)->{
            redisConnection.serverCommands().flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("k","111");
        redisTemplate.opsForList().leftPush("k","222");
        redisTemplate.opsForList().leftPush("k","333");
        String s = redisTemplate.opsForList().rightPop("k");
        System.out.println(s);

        return"";
    }

    @RequestMapping("/testHash")
    @ResponseBody
    public String testHash(){
        redisTemplate.execute((RedisConnection redisConnection)->{
           redisConnection.serverCommands().flushAll();
           return null;
        });
        redisTemplate.opsForHash().put("k2","f1","111");
        redisTemplate.opsForHash().put("k2","f2","222");
        redisTemplate.opsForHash().put("k2","f3","333");
        Object o = redisTemplate.opsForHash().get("k2", "f1");
        System.out.println(o);
        Boolean b = redisTemplate.opsForHash().hasKey("k2", "f1");
        System.out.println(b);
        redisTemplate.opsForHash().delete("k2","f3","f1");
        Long k2 = redisTemplate.opsForHash().size("k2");
        System.out.println(k2);


        return null;
    }

    @RequestMapping("/testSet")
    @ResponseBody
    public String testSet(){
        redisTemplate.execute((RedisConnection redisConnection)->{
            redisConnection.serverCommands().flushAll();
            return null;
        });
        redisTemplate.opsForSet().add("k1","111","222","333");
        Set<String> k1 = redisTemplate.opsForSet().members("k1");
        System.out.println(k1);
        Boolean k11 = redisTemplate.opsForSet().isMember("k1", "111");
        System.out.println(k11);
        Long k12 = redisTemplate.opsForSet().size("k1");
        System.out.println(k12);
        redisTemplate.opsForSet().remove("k1","222");
        Set<String> k13 = redisTemplate.opsForSet().members("k1");
        System.out.println(k13);

        return"";
    }

    @RequestMapping("/testZSet")
    @ResponseBody
    public String testZSet(){
        redisTemplate.execute((RedisConnection redisConnection)->{
            redisConnection.serverCommands().flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("k3","zhangsan",11);
        redisTemplate.opsForZSet().add("k3","lsii",22);
        redisTemplate.opsForZSet().add("k3","wangwu",33);
        Set<String> key = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println(key);
        Set<ZSetOperations.TypedTuple<String>> k3 = redisTemplate.opsForZSet().rangeWithScores("k3", 0, -1);
        System.out.println(k3);
        Double score = redisTemplate.opsForZSet().score("k3", "zhangsan");
        System.out.println(score);
        Long remove = redisTemplate.opsForZSet().remove("k3", "wangwu");
        System.out.println(remove);
        Long k31 = redisTemplate.opsForZSet().size("k3");
        System.out.println(k31);
        Long rank = redisTemplate.opsForZSet().rank("k3", "zhangsan");
        System.out.println(rank);

        return"";
    }
}
